<?php
/**
 * @title 1443. 收集树上所有苹果的最少时间
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $edges
     * @param Boolean[] $hasApple
     * @return Integer
     */
    function minTime($n, $edges, $hasApple) {
        /**
         * @since 2024-05-13 苹果的数量
         */
        $appleCnt = 0;
        foreach ($hasApple as $has){
            if($has) $appleCnt++;
        }

        /**
         * @since 2024-05-13 点到点的关系
         */
        $line = [];
        foreach ($edges as list($f, $t)){
            $line[$f][$t] = $t;
            $line[$t][$f] = $f;
        }

        /**
         * @since 2024-05-13 经过的路线
         */
        $path = [];

        /**
         * @since 2024-05-13 BFS广度遍历
         */
        $nodeArray = [
            [0, []],
        ];
        while($nodeArray AND $appleCnt > 0){
            /**
             * @since 2024-05-13 下次遍历元素的集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-05-13 遍历当前元素集合
             */
            foreach ($nodeArray as list($node, $parentPathArray)){
                if($hasApple[$node] == true){
                    $appleCnt--;
                    // foreach ($parentPathArray as $p){
                    //     $path[$p] = true;
                    // }

                    /**
                     * @since 2024-05-13 自上而下路径，遇到相同路径，向上路径一致，不再计算
                     */
                    for($i=count($parentPathArray)-1; $i>=0; $i--){
                        $p = $parentPathArray[$i];
                        if(isset($path[$p])){
                            break;
                        } else {
                            $path[$p] = true;
                        }
                    }
                } else {}

                /**
                 * @since 2024-05-13 下次遍历的元素
                 */
                foreach($line[$node] as $nextNode){
                    $nextNodeArray[] = [$nextNode, array_merge($parentPathArray, ["{$node}-{$nextNode}"])];
                    unset($line[$nextNode][$node]);
                }
            }

            /**
             * @since 2024-05-13 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return
         */
        return count($path)*2;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.13_4.php
 */
$datas = [
    [7, [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], [false,false,true,false,true,true,false]], // 8
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->minTime(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
